package at.fhj.itm11.forum.model;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;

@Entity
public class Post implements Serializable {

	private static final long serialVersionUID = 4982114296729925223L;
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "id", updatable = false, nullable = false)
	private Long id = null;
	@Version
	@Column(name = "version")
	private int version = 0;

	@Column
	@NotNull
	private String title;

	@Temporal(TemporalType.TIMESTAMP)
	private Date date;

	@Column(length=1500) //maximum 1500 letters
	@NotNull
	private String text;

	@Lob
	@Column(length = 16777215)
	private byte[] image;
	
	@Lob
	@Column(length = 16777215)
	private byte[] video;

	@OneToOne
	@NotNull
	private User user;

	@ManyToOne
	@JoinColumn(name = "topic_id")
	@NotNull
	private Topic topic;

	@Column
	@NotNull
	private boolean active;

	public Long getId() {
		return this.id;
	}

	public void setId(final Long id) {
		this.id = id;
	}

	public int getVersion() {
		return this.version;
	}

	public void setVersion(final int version) {
		this.version = version;
	}

	@Override
	public boolean equals(Object that) {
		if (this == that) {
			return true;
		}
		if (that == null) {
			return false;
		}
		if (getClass() != that.getClass()) {
			return false;
		}
		if (id != null) {
			return id.equals(((Post) that).id);
		}
		return super.equals(that);
	}

	@Override
	public int hashCode() {
		if (id != null) {
			return id.hashCode();
		}
		return super.hashCode();
	}

	public String getTitle() {
		return this.title;
	}

	public void setTitle(final String title) {
		this.title = title;
	}

	public Date getDate() {
		return this.date;
	}

	public void setDate(final Date date) {
		this.date = date;
	}

	public String getText() {
		return this.text;
	}

	public void setText(final String text) {
		this.text = text;
	}

	public byte[] getImage() {
		return image;
	}

	public void setImage(byte[] image) {
		if (image != null) {
			this.image = image.clone();
		} else {
			this.image = null;
		}
	}
	
	public byte[] getVideo() {
		return video;
	}

	public void setVideo(byte[] video) {
		if (video != null) {
			this.video = video.clone();
		} else {
			this.video = null;
		}
	}

	public User getUser() {
		return this.user;
	}

	public void setUser(final User user) {
		this.user = user;
	}

	public Topic getTopic() {
		return this.topic;
	}

	public void setTopic(final Topic topic) {
		this.topic = topic;
	}

	public boolean getActive() {
		return this.active;
	}

	public void setActive(final boolean active) {
		this.active = active;
	}

	@Override
	public String toString() {
		String result = getClass().getSimpleName() + " ";
		result += "serialVersionUID: " + serialVersionUID;
		if (id != null)
			result += ", id: " + id;
		if (title != null && !title.trim().isEmpty())
			result += ", title: " + title;
		if (text != null && !text.trim().isEmpty())
			result += ", text: " + text;
		result += ", active: " + active;
		return result;
	}
}
